﻿using System.Text.Json;

using XServer.Models.OSS;
using XServer.Utility;

namespace XServer.Managers
{
    public class OssManager : Singleton<OssManager>
    {
        private OssConfig _config;

        public OssManager()
        {
            string content = File.ReadAllText("./configs/oss.json");
            _config = JsonSerializer.Deserialize<OssConfig>(content) ?? new OssConfig();
        }

        /// <summary>
        /// 获取OSS配置
        /// </summary>
        /// <returns></returns>
        public OssConfig GetConfig()
        {
            return _config;
        }

        /// <summary>
        /// 重新加载OSS配置
        /// </summary>
        /// <returns></returns>
        public OssConfig ReLoad()
        {
            string content = File.ReadAllText("./configs/oss.json");
            _config = JsonSerializer.Deserialize<OssConfig>(content) ?? new OssConfig();
            return _config;
        }

        /// <summary>
        /// 获取文件Key
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public string GetFileKey(string filePath)
        {
            string fileName = Path.GetFileName(filePath);
            string key = Path.Combine(_config.PathFilter, fileName);
            return key;
        }

        /// <summary>
        /// 检查文件是否存在
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public bool CheckFileExist(string filePath)
        {
            string key = GetFileKey(filePath);
            return OssHelper.CheckObjectExist(_config, key);
        }
    }
}
